library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_unsigned.all;

entity show_control is
port(
	clk:in std_logic;
	clk_4hz:in std_logic;
	pulse:in std_logic;
	cnt_qiehuan:in integer range 0 to 2;
	row:out std_logic_vector(7 downto 0);
	red:out std_logic_vector(7 downto 0);
	green:out std_logic_vector(7 downto 0);
	state:in integer range 0 to 4
	);
	
end;

architecture struct of show_control is
signal temp_clk,breath_clk:std_logic;
signal count:integer range 0 to 39999;
signal count_sweep:integer range 0 to 8;
signal out_row:std_logic_vector(7 downto 0);
signal out_r:std_logic_vector(7 downto 0);
signal out_g:std_logic_vector(7 downto 0);
signal count_4hz:integer range 0 to 7;
--signal count_breath:integer range 0 to 100;


begin
	p1:process(clk)
	begin
		if(clk'event and clk='1')then
			if count=39999 then
				count<=0;
				temp_clk<=not temp_clk;
			else
				count<=count+1;
			end if;
		end if;
	end process p1;
	
	p2:process(temp_clk)  --sweep count
	begin
		if(temp_clk'event and temp_clk='1')then
			if count_sweep=8 then
				count_sweep<=0;
			else
				count_sweep<=count_sweep+1;
			end if;
		end if;
	end process p2;
	
	p3:process(pulse,cnt_qiehuan,count_4hz,state)
	begin
	case state is --0:opening music,1:running,2:turn_left,3:turn_right,4:stop
		when 0=>
			out_row<="11111111";
		
		when 1=>
		case cnt_qiehuan is
			when 0=>
				if pulse='1' then
					case count_sweep is--GO
						when 0=>out_row<="01111111";out_r<="00111110";                 
						when 1=>out_row<="10111111";out_r<="01000010";
						when 2=>out_row<="11011111";out_r<="00100010";
						when 3=>out_row<="11101111";out_r<="00011110";
						when 4=>out_row<="11110111";out_r<="00001010";
						when 5=>out_row<="11111011";out_r<="00010010";
						when 6=>out_row<="11111101";out_r<="00100010";
						when 7=>out_row<="11111110";out_r<="01000010";
						when others=>out_row<="11111111";out_r<="00000000";
					end case;
				else
					out_row<="11111111";out_r<="00000000";
				end if;
				
				when 1=>
					if pulse='1' then
					case count_sweep is--GO
						when 0=>out_row<="01111111";out_r<="01000010";                
						when 1=>out_row<="10111111";out_r<="01000010";
						when 2=>out_row<="11011111";out_r<="01000010";
						when 3=>out_row<="11101111";out_r<="01000010";
						when 4=>out_row<="11110111";out_r<="01000010";
						when 5=>out_row<="11111011";out_r<="01000010";
						when 6=>out_row<="11111101";out_r<="01000010";
						when 7=>out_row<="11111110";out_r<="00111100";
						when others=>out_row<="11111111";out_r<="00000000";
					end case;
				else
					out_row<="11111111";out_r<="00000000";
				end if;
				
				when 2=>
					if pulse='1' then
					case count_sweep is--GO
						when 0=>out_row<="01111111";out_r<="10000001";                 
						when 1=>out_row<="10111111";out_r<="10000011";
						when 2=>out_row<="11011111";out_r<="10000101";
						when 3=>out_row<="11101111";out_r<="10001001";
						when 4=>out_row<="11110111";out_r<="10010001";
						when 5=>out_row<="11111011";out_r<="10100001";
						when 6=>out_row<="11111101";out_r<="11000001";
						when 7=>out_row<="11111110";out_r<="10000001";
						when others=>out_row<="11111111";out_r<="00000000";
					end case;
				else
					out_row<="11111111";out_r<="00000000";
				end if;
				
				end case;
		
		when 2=>
		
		case count_4hz is
			when 0=>
					case count_sweep is--GO
						when 0=>out_row<="01111111";out_r<="00000000";                 
						when 1=>out_row<="10111111";out_r<="00001000";
						when 2=>out_row<="11011111";out_r<="11111100";
						when 3=>out_row<="11101111";out_r<="11111110";
						when 4=>out_row<="11110111";out_r<="11111111";
						when 5=>out_row<="11111011";out_r<="11111110";
						when 6=>out_row<="11111101";out_r<="00001100";
						when 7=>out_row<="11111110";out_r<="00001000";
						when others=>out_row<="11111111";out_r<="00000000";
					end case;
			
			when 1=>
					case count_sweep is--GO
						when 0=>out_row<="01111111";out_r<="00000000";                 
						when 1=>out_row<="10111111";out_r<="00000100";
						when 2=>out_row<="11011111";out_r<="01111110";
						when 3=>out_row<="11101111";out_r<="01111111";
						when 4=>out_row<="11110111";out_r<="11111111";
						when 5=>out_row<="11111011";out_r<="01111111";
						when 6=>out_row<="11111101";out_r<="00000110";
						when 7=>out_row<="11111110";out_r<="00000100";
						when others=>out_row<="11111111";out_r<="00000000";
					end case;
					
			when 2=>
					case count_sweep is--GO
						when 0=>out_row<="01111111";out_r<="00000000";                 
						when 1=>out_row<="10111111";out_r<="00000010";
						when 2=>out_row<="11011111";out_r<="00111111";
						when 3=>out_row<="11101111";out_r<="10111111";
						when 4=>out_row<="11110111";out_r<="11111111";
						when 5=>out_row<="11111011";out_r<="10111111";
						when 6=>out_row<="11111101";out_r<="00000011";
						when 7=>out_row<="11111110";out_r<="00000010";
						when others=>out_row<="11111111";out_r<="00000000";
					end case;
			
			when 3=>
					case count_sweep is--GO
						when 0=>out_row<="01111111";out_r<="00000000";                 
						when 1=>out_row<="10111111";out_r<="00000001";
						when 2=>out_row<="11011111";out_r<="10011111";
						when 3=>out_row<="11101111";out_r<="11011111";
						when 4=>out_row<="11110111";out_r<="11111111";
						when 5=>out_row<="11111011";out_r<="11011111";
						when 6=>out_row<="11111101";out_r<="10000001";
						when 7=>out_row<="11111110";out_r<="00000001";
						when others=>out_row<="11111111";out_r<="00000000";
					end case;
					
			when 4=>
					case count_sweep is--GO
						when 0=>out_row<="01111111";out_r<="00000000";                 
						when 1=>out_row<="10111111";out_r<="10000000";
						when 2=>out_row<="11011111";out_r<="11001111";
						when 3=>out_row<="11101111";out_r<="11101111";
						when 4=>out_row<="11110111";out_r<="11111111";
						when 5=>out_row<="11111011";out_r<="11101111";
						when 6=>out_row<="11111101";out_r<="11000000";
						when 7=>out_row<="11111110";out_r<="10000000";
						when others=>out_row<="11111111";out_r<="00000000";
					end case;
			
			when 5=>
					case count_sweep is--GO
						when 0=>out_row<="01111111";out_r<="00000000";                 
						when 1=>out_row<="10111111";out_r<="01000000";
						when 2=>out_row<="11011111";out_r<="11100111";
						when 3=>out_row<="11101111";out_r<="11110111";
						when 4=>out_row<="11110111";out_r<="11111111";
						when 5=>out_row<="11111011";out_r<="11110111";
						when 6=>out_row<="11111101";out_r<="01100000";
						when 7=>out_row<="11111110";out_r<="01000000";
						when others=>out_row<="11111111";out_r<="00000000";
					end case;
					
			when 6=>
					case count_sweep is--GO
						when 0=>out_row<="01111111";out_r<="00000000";                 
						when 1=>out_row<="10111111";out_r<="00100000";
						when 2=>out_row<="11011111";out_r<="11110011";
						when 3=>out_row<="11101111";out_r<="11111011";
						when 4=>out_row<="11110111";out_r<="11111111";
						when 5=>out_row<="11111011";out_r<="11111011";
						when 6=>out_row<="11111101";out_r<="00110000";
						when 7=>out_row<="11111110";out_r<="00100000";
						when others=>out_row<="11111111";out_r<="00000000";
					end case;
			
			when 7=>
					case count_sweep is--GO
						when 0=>out_row<="01111111";out_r<="00000000";                 
						when 1=>out_row<="10111111";out_r<="00010000";
						when 2=>out_row<="11011111";out_r<="11111001";
						when 3=>out_row<="11101111";out_r<="11111101";
						when 4=>out_row<="11110111";out_r<="11111111";
						when 5=>out_row<="11111011";out_r<="11111101";
						when 6=>out_row<="11111101";out_r<="00011000";
						when 7=>out_row<="11111110";out_r<="00010000";
						when others=>out_row<="11111111";out_r<="00000000";
					end case;
			end case;
		when 3=>
			case count_4hz is
			when 0=>
					case count_sweep is--GO
						when 0=>out_row<="01111111";out_r<="00000000";                 
						when 1=>out_row<="10111111";out_r<="00010000";
						when 2=>out_row<="11011111";out_r<="00111111";
						when 3=>out_row<="11101111";out_r<="01111111";
						when 4=>out_row<="11110111";out_r<="11111111";
						when 5=>out_row<="11111011";out_r<="01111111";
						when 6=>out_row<="11111101";out_r<="00110000";
						when 7=>out_row<="11111110";out_r<="00010000";
						when others=>out_row<="11111111";out_r<="00000000";
					end case;
			
			when 1=>
					case count_sweep is--GO
						when 0=>out_row<="01111111";out_r<="00000000";                 
						when 1=>out_row<="10111111";out_r<="00100000";
						when 2=>out_row<="11011111";out_r<="01111110";
						when 3=>out_row<="11101111";out_r<="11111110";
						when 4=>out_row<="11110111";out_r<="11111111";
						when 5=>out_row<="11111011";out_r<="11111110";
						when 6=>out_row<="11111101";out_r<="01100000";
						when 7=>out_row<="11111110";out_r<="00100000";
						when others=>out_row<="11111111";out_r<="00000000";
					end case;
					
			when 2=>
					case count_sweep is--GO
						when 0=>out_row<="01111111";out_r<="00000000";                 
						when 1=>out_row<="10111111";out_r<="01000000";
						when 2=>out_row<="11011111";out_r<="11111100";
						when 3=>out_row<="11101111";out_r<="11111101";
						when 4=>out_row<="11110111";out_r<="11111111";
						when 5=>out_row<="11111011";out_r<="11111101";
						when 6=>out_row<="11111101";out_r<="11000000";
						when 7=>out_row<="11111110";out_r<="01000000";
						when others=>out_row<="11111111";out_r<="00000000";
					end case;
			
			when 3=>
					case count_sweep is--GO
						when 0=>out_row<="01111111";out_r<="00000000";                 
						when 1=>out_row<="10111111";out_r<="10000000";
						when 2=>out_row<="11011111";out_r<="11111001";
						when 3=>out_row<="11101111";out_r<="11111011";
						when 4=>out_row<="11110111";out_r<="11111111";
						when 5=>out_row<="11111011";out_r<="11111011";
						when 6=>out_row<="11111101";out_r<="10000001";
						when 7=>out_row<="11111110";out_r<="10000000";
						when others=>out_row<="11111111";out_r<="00000000";
					end case;
					
			when 4=>
					case count_sweep is--GO
						when 0=>out_row<="01111111";out_r<="00000000";                 
						when 1=>out_row<="10111111";out_r<="00000001";
						when 2=>out_row<="11011111";out_r<="11110011";
						when 3=>out_row<="11101111";out_r<="11110111";
						when 4=>out_row<="11110111";out_r<="11111111";
						when 5=>out_row<="11111011";out_r<="11110111";
						when 6=>out_row<="11111101";out_r<="00000011";
						when 7=>out_row<="11111110";out_r<="00000001";
						when others=>out_row<="11111111";out_r<="00000000";
					end case;
			
			when 5=>
					case count_sweep is--GO
						when 0=>out_row<="01111111";out_r<="00000000";                 
						when 1=>out_row<="10111111";out_r<="00000010";
						when 2=>out_row<="11011111";out_r<="11100111";
						when 3=>out_row<="11101111";out_r<="11101111";
						when 4=>out_row<="11110111";out_r<="11111111";
						when 5=>out_row<="11111011";out_r<="11101111";
						when 6=>out_row<="11111101";out_r<="00000110";
						when 7=>out_row<="11111110";out_r<="00000010";
						when others=>out_row<="11111111";out_r<="00000000";
					end case;
					
			when 6=>
					case count_sweep is--GO
						when 0=>out_row<="01111111";out_r<="00000000";                 
						when 1=>out_row<="10111111";out_r<="00000100";
						when 2=>out_row<="11011111";out_r<="11001111";
						when 3=>out_row<="11101111";out_r<="11011111";
						when 4=>out_row<="11110111";out_r<="11111111";
						when 5=>out_row<="11111011";out_r<="11011111";
						when 6=>out_row<="11111101";out_r<="00001100";
						when 7=>out_row<="11111110";out_r<="00000100";
						when others=>out_row<="11111111";out_r<="00000000";
					end case;
			
			when 7=>
					case count_sweep is--GO
						when 0=>out_row<="01111111";out_r<="00000000";                 
						when 1=>out_row<="10111111";out_r<="00001000";
						when 2=>out_row<="11011111";out_r<="10011111";
						when 3=>out_row<="11101111";out_r<="10111111";
						when 4=>out_row<="11110111";out_r<="11111111";
						when 5=>out_row<="11111011";out_r<="10111111";
						when 6=>out_row<="11111101";out_r<="00011000";
						when 7=>out_row<="11111110";out_r<="00001000";
						when others=>out_row<="11111111";out_r<="00000000";
					end case;
			end case;
						
		when 4=>
			case count_sweep is--GO
						when 0=>out_row<="01111111";out_r<="10000001";                 
						when 1=>out_row<="10111111";out_r<="01000010";
						when 2=>out_row<="11011111";out_r<="00100100";
						when 3=>out_row<="11101111";out_r<="00011000";
						when 4=>out_row<="11110111";out_r<="00011000";
						when 5=>out_row<="11111011";out_r<="00100100";
						when 6=>out_row<="11111101";out_r<="01000010";
						when 7=>out_row<="11111110";out_r<="10000001";
						when others=>out_row<="11111111";out_r<="00000000";
					end case;
		end case;
			
	end process;
	
	
	p4:process(clk_4hz)	
	begin 
		if(clk_4hz'event and clk_4hz='1') then
			if(count_4hz=7) then 
				count_4hz<=0;
			else
				count_4hz<=count_4hz+1;
			end if;
		end if;
	end process p4;
	
		
		
	--p4:process(clk_2)--count_breath
	--begin 
		--if(temp_clk'event and temp_clk='1')then
			--if count_breath=100 then
				--count_breath<=0;
			--else
				--count_breath<=count_breath+1;
			--end if;
		--end if;
	--end process p4;
	
	--p5:process(clk_2,count_breath)
		--begin
		--if(clk_2'event and clk_2='1')then
			--if count=count_breath then
				--count<=0;
				--breath_clk<=not breath_clk;
			--else
				--count<=count+1;
			--end if;
		--end if;
	--end process p5;	
	
	
	
	red<=out_r;
	green<="00000000";
	row<=out_row;
	end struct;